define(function(require,exports,module) {
    var myPub = require('./public');

    $(function() {
        var oFooter = $('.js_footer');              //底部
        var oLoadingPage =$('.js_loadingPage');

        var oDefaultAddr = $('.js_DefaultAddr');	//默认地址选择
        var oClearBtn = $('.js_clearBtn');		//清除按钮

        var oUserName = $('.js_userName');  	//联系人姓名
        var oUserIphone = $('.js_userIphone');	//联系人电话

        var addressInit = $('.initAddress');
        var provinceInput = $('.js_province');
        var cityInput = $('.js_city');
        var areaInput = $('.js_area');
        var addressInput = $('.js_address');
        var saveBtn = $('.js_saveBtn');
        var oldAddress = $('.jsOldDetailAddress');

        var userDetailAddress = $('.jsUserDetailAddress');

        var btnOff = true;

        oFooter.on("touchmove",function(e) {e.preventDefault()});
        oLoadingPage.on('touchmove',function() {return false});

        var streetName = "";
        var addressName = "";

        var oldAddressVal = oldAddress.val();
        if(oldAddressVal != null && oldAddressVal.length > 0){
            var index = oldAddressVal.indexOf(" ");
            streetName = oldAddressVal.substring(0,index);
            addressName = oldAddressVal.substring(index + 1,oldAddressVal.length);

            // $('.js_cityStreetResult').val(streetName);
            $('.jsUserDetailAddress').val(addressName);
        }



        //默认地址选择
        oDefaultAddr.hammer().on('tap',function() {
            //关闭mui tap 事件
            myPub.OffMuiEvent(false)
            if(oDefaultAddr.hasClass('active')) {
                oDefaultAddr.removeClass('active')
            }
            else {
                oDefaultAddr.addClass('active')
            }
        });



        //联系人电话检测
        oUserIphone.on('input propertychange', function() {
            oUserIphone.siblings('.js_clearBtn').show();

            myPub.checkMobile(oUserIphone,function() {
                myPub.TipLayer('联系人手机号码不能为空！');
                oUserIphone.parents('.js_userAddrInfo').removeClass('has_error').removeClass('has_success');
                oUserIphone.siblings('.js_clearBtn').hide();
                oUserIphone.attr('judgeBloor',false)
            },function() {
                oUserIphone.parents('.js_userAddrInfo').addClass('has_error').removeClass('has_success')
                oUserIphone.attr('judgeBloor',false)
            },function() {
                oUserIphone.parents('.js_userAddrInfo').addClass('has_success').removeClass('has_error');
                oUserIphone.attr('judgeBloor',true)
            });

        })



        //联系人姓名检测
        oUserName.on('input propertychange', function() {
            oUserName.siblings('.js_clearBtn').show();

            myPub.checkUserName(oUserName,function() {
                myPub.TipLayer('联系人姓名不能为空！');
                oUserName.parents('.js_userAddrInfo').removeClass('has_error').removeClass('has_success');
                oUserName.siblings('.js_clearBtn').hide();
                oUserName.attr('judgeBloor',false)
            },function() {
               // oUserName.parents('.js_userAddrInfo').addClass('has_error').removeClass('has_success')
                oUserName.attr('judgeBloor',false)
            },function() {
               // oUserName.parents('.js_userAddrInfo').addClass('has_success').removeClass('has_error');
                oUserName.attr('judgeBloor',true)
            });

        });

        saveBtn.on('click',function(){
            if($.trim($('input[name="contactName"]').val()) == ""){
                myPub.TipLayer("请输入联系人姓名");
                return false;
            }
            if($.trim($('input[name="mobile"]').val()) == ""){
                myPub.TipLayer("请输入联系人手机号码");
                return false;
            }
            if($('input[name="province"]').val() == "" || $('input[name="city"]').val() == ""){
                myPub.TipLayer("请选择所在地区信息");
                return false;
            }
            if($.trim($('textarea[name="userAddress"]').val()) == ""){
                myPub.TipLayer("请填写详细地址信息");
                return false;
            }
            var reg = /^(1[3|4|5|7|8])[\d]{9}$/;   //验证手机号码
            if(!reg.test($('input[name="mobile"]').val()) ){
                myPub.TipLayer("请输入正确的手机号码");
                return false;
            }
            if($('.js_DefaultAddr').hasClass('active')){
                $('.js_default').val('true');
            }else{
                $('.js_default').val('false');
            }
            var sStreet = $('.js_cityStreetResult').val();
            console.log(sStreet);

            if(btnOff) {
                btnOff = false;
                sendData();
            }
            addressInput.val(sStreet + " " + userDetailAddress.val());
            // $('.js_address_form').submit();
        });


        function  sendData() {
            G_JSON_POST("/express/bespeakExpress", $('#bespeakForm').serialize(), function (data) {
                //console.log(data);
                if(data.hasError == undefined || data.hasError == false){
                    oUserName.val('');
                    oUserIphone.val('');
                    $('.js_cityResult').val('');
                    $('.js_cityStreetResult').val('');
                    $('#goodsName').val('');
                    var message="<div class='indexAddr'>您已成功下单，订单号："+data.orderNo+"。顺丰收派员将在1小时内上门收件（偏远地区以及节假日需加时），请耐心等候，如有疑问请联系顺丰客服电话95338。</div>"
                    myPub.TipBtnLayer(message,'no','我知道了',function () {
                        myPub.LayerCloseAll();
                        btnOff = true;
                    });
                }else{
                    myPub.TipBtnLayer(data.errorMsg,'no','我知道了',function () {
                        myPub.LayerCloseAll();
                        btnOff = true
                    });
                }
            });
        }


        //选择城市
        (function($$) {
            // $$.init();
            $$.ready(function() {
                var cityPicker = new $$.PopPicker({  layer: 3  });
                var cityStreetPicker = new $$.PopPicker({  layer: 1  });

                cityPicker.setData(cityData3);

                var showCityPickerButton = $('.js_showCityBtn')[0];
                var showCityPickerStreetButton = $('.js_showCityStreetBtn')[0];
                var cityResult = $('.js_cityResult');
                var cityStreetResult = $('.js_cityStreetResult');
                var oPicker = $('.mui-poppicker .mui-picker');


                //有地址
                cityResult.val().trim() !== '' ? OldCitySet() : cityResult.val('');



                showCityPickerButton.addEventListener('click', function(event) {
                    //开启mui tap 事件
                    myPub.OffMuiEvent(true);
                    cityPicker.show(function(items) {
                        var province = ( items[0] || {}).text;
                        var city = ( items[1] || {}).text;
                        var area = ( items[2] || {}).text;


                        if(area == undefined) {area = ''}
                        var _seletCity = province + " " + city + " " + area;
                        cityResult[0].value = _seletCity;

                        cityStreetPicker.setData(items[2].children);
                        cityStreetResult[0].value = ''

                        //城市 省份 地区
                        provinceInput.val(province);
                        cityInput.val(city);
                        areaInput.val(area);

                    });
                }, false);


                showCityPickerStreetButton.addEventListener('click', function(event) {
                    //开启mui tap 事件
                    myPub.OffMuiEvent(true);
                    if(cityResult[0].value == '') {
                        myPub.TipLayer('请先选择地区！');
                    }
                    else{
                        cityStreetPicker.show(function(items) {
                            var sStreet = ( items[0] || {}).text;
                            if(sStreet == undefined) {sStreet=''}
                            cityStreetResult[0].value = sStreet;
                        });

                    }
                }, false);
                if(addressInit.length > 0){
                    cityResult[0].value = addressInit.text();
                }

                //旧地址数据定位设置
                function OldCitySet() {
                    var _OldAddr = cityResult.val().split(' ');
                    var get_firstName = _OldAddr[0].substr(0,_OldAddr[0].length-1);
                    var get_secondName = _OldAddr[1].substr(0,_OldAddr[1].length-1);
                    var get_threeName = _OldAddr[2].substr(0,_OldAddr[2].length-1);

                    var date_prov = cityPicker.pickers[0].items
                    for (var i=0; i<date_prov.length; i++) {
                        var _provName = date_prov[i].text.substr(0,date_prov[i].text.length-1)
                        if(_provName == get_firstName) {
                            cityPicker.pickers[0].setAngle(cityPicker.pickers[0].elementItems[i].angle);
                        }
                    }

                    setTimeout(function() {
                        var date_city = cityPicker.pickers[1].items;
                        for (var i=0; i<date_city.length; i++) {
                            var _cityName = date_city[i].text.substr(0,date_city[i].text.length-1)
                            if(_cityName == get_secondName) {
                                cityPicker.pickers[1].setAngle(cityPicker.pickers[1].elementItems[i].angle);

                            }
                        }
                    },10);


                    setTimeout(function() {
                        var date_area = cityPicker.pickers[2].items;
                        for (var i=0; i<date_area.length; i++) {
                            var _areaName = date_area[i].text.substr(0,date_area[i].text.length-1)
                            if(_areaName == get_threeName) {
                                cityPicker.pickers[2].setAngle(cityPicker.pickers[2].elementItems[i].angle);
                            }
                        }

                    },80);


                    setTimeout(function() {
                        cityPicker.callback = function(items) {
                            var province = ( items[0] || {}).text;
                            var city = ( items[1] || {}).text;
                            var area = ( items[2] || {}).text;

                            if(area == undefined) {area = ''}
                            var _seletCity = province + " " + city + " " + area;
                            cityResult[0].value = _seletCity;
                            cityStreetPicker.setData(items[2].children);
                            cityStreetResult[0].value = '';
                            //城市 省份 地区
                            provinceInput.val(province);
                            cityInput.val(city);
                            areaInput.val(area);

                        }
                        mui.trigger(cityPicker.ok,'tap');
                    },100);


                    setTimeout(function () {
                        var date_street = cityStreetPicker.pickers[0].items;
                        var flag = false;
                        for (var i=0; i<date_street.length; i++) {
                            var _streetName = date_street[i].text;
                            // console.log($('.js_cityStreetResult').val() + "+   aaa");
                            if(_streetName == streetName) {
                                cityStreetPicker.pickers[0].setAngle(cityStreetPicker.pickers[0].elementItems[i].angle);
                                $('.js_cityStreetResult').val(_streetName);
                                flag = true;
                            }
                        }
                        if(flag == false){
                            if(streetName != null && streetName.length > 0){
                                $('.jsUserDetailAddress').val(streetName + " " +  addressName);
                            }
                        }
                    },150)

                }

            });
        })(mui);

    })




});


